/**
 * 模拟迭代器
 * @param {*} arr
 */
const iterator = (arr = []) => {
  let nextIndex = 0;
  return {
    next() {
      if (this.hasNext()) return { value: arr[nextIndex++], done: false };
      return { value: undefined, done: true };
    },
    hasNext() {
      return nextIndex < arr.length;
    },
  };
};
let it = iterator([10, 20]);
while (it.hasNext()) {
  console.log(it.next()); // { value: 10, done: false } { value: 20, done: false }
}
